﻿using EkStudio.com.bean.xml.BillMapping;
using EkStudio.com.ui.unitbill.property.Event;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace EkStudio.com.ui.unitbill.property
{
    public class BillMapUiGroupToProperty
    {
        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(BillMapUiGroupToProperty));

        private BillDataMapGroupItem data = null;
        private BillMapUiGroupToProperty[] array = null;

        //public event KEventHandler DataChange = null;

        private bool Simple = true;

        public BillMapUiGroupToProperty(BillDataMapGroupItem item)
        {
            data = item;
        }

        public BillMapUiGroupToProperty(BillMapUiGroupToProperty[] list)
        {
            array = list;
            Simple = false;
        }


        public string Caption
        {
            get { return Simple ? data.Caption : ""; }
            set { if (Simple) data.Caption = value; else SetValue("Caption", value); }
        }

        public string Key
        {
            get { return Simple ? data.Key : ""; }
            set { if (Simple) data.Key = value; else SetValue("Key", value); }
        }
        [DescriptionAttribute("当前模块所更新的库存分组")]
        public string GroupType
        {
            get { return Simple ? data.GroupType : GetStringValue("GroupType"); }
            set { if (Simple) data.GroupType = value; else SetValue("GroupType", value); }
        }


        public string Config
        {
            get { return  data.TargetCaption==""?"[NULL]":string.Format("[{0}]",data.TargetCaption); }
            set { data.TargetCaption = value; }
        }
        [DescriptionAttribute("更新库存成功后的状态")]
        public string Success
        {
            get { return data.StatusSuccess; }
            set { data.StatusSuccess = value; }
        }
        [DescriptionAttribute("未更新或者更新失败后的状态")]
        public string Fail
        {
            get { return data.StatusFail; }
            set { data.StatusFail = value; }
        }
        [DescriptionAttribute("更新库存时执行的扩展代码")]
        public string Plugin
        {
            get { return data.PluginExpand; }
            set { data.PluginExpand = value; }
        }





        private object SetValue(string key, object value)
        {
            if (this.array != null && this.array.Length > 0)
            {
                for (int i = 0; i < this.array.Length; i++)
                {
                    BillMapUiGroupToProperty dt = array[i];
                    Type ClazzType = typeof(BillMapUiGroupToProperty);
                    PropertyInfo Property = ClazzType.GetProperty(key);
                    if (Property != null)
                    {
                        Property.SetValue(dt, value);
                    }
                }
            }
            return value;
        }

        private bool GetBoolValue(string key)
        {
            bool result = true;
            if (this.array != null && this.array.Length > 0)
            {
                for (int i = 0; i < this.array.Length; i++)
                {

                    BillMapUiGroupToProperty data = array[i];
                    Type ClazzType = typeof(BillMapUiGroupToProperty);
                    PropertyInfo Property = ClazzType.GetProperty(key);
                    if (Property != null && Property.GetMethod.ReturnType == typeof(bool))
                    {
                        object val = Property.GetValue(data);
                        if (val.Equals(false))
                        {
                            result = false;
                            break;
                        }
                    }
                }
            }

            return result;
        }

        private int GetIntValue(string key)
        {
            int result = 0;
            if (this.array != null && this.array.Length > 0)
            {
                BillMapUiGroupToProperty data = array[0];
                Type ClazzType = typeof(BillMapUiGroupToProperty);
                PropertyInfo Property = ClazzType.GetProperty(key);
                if (Property != null && Property.GetMethod.ReturnType == typeof(int))
                {
                    object val = Property.GetValue(data);
                    if (val is int)
                    {
                        result = (int)val;
                    }
                }
            }
            return result;
        }

        private string GetStringValue(string key)
        {
            string result = "";
            if (this.array != null && this.array.Length > 0)
            {
                for (int i = 0; i < this.array.Length; i++)
                {
                    BillMapUiGroupToProperty data = array[i];
                    Type ClazzType = typeof(BillMapUiGroupToProperty);
                    PropertyInfo Property = ClazzType.GetProperty(key);
                    if (Property != null && Property.GetMethod.ReturnType == typeof(string))
                    {
                        object val = Property.GetValue(data);
                        if (val is string)
                        {
                            if (result == "") result = val.ToString();
                            else if (result != (string)val)
                            {
                                result = "";
                                break;
                            }

                        }
                    }
                }
            }
            return result;
        }
    }
}
